import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {JoinDeskComp} from "db://assets/script/game/dating/comp/joinDeskComp";
import {api} from "db://assets/script/game/player/api";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {GameType} from "db://assets/script/game/constants/common";
import {DeskNoKeyboardView} from "db://assets/script/game/common/view/deskNoKeyboardView";
import {oops} from "db://oops-framework/core/Oops";
import {UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {pb} from "db://assets/script/network/ws/proto";

export class JoinDeskSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {

    filter(): ecs.IMatcher {
        return ecs.allOf(JoinDeskComp)
    }

    entityEnter(entity: ecs.Entity) {
        const comp = entity.get(JoinDeskComp)
        const removeJoinDeskComp = () => {
            smc.dating.remove(JoinDeskComp)
            const view = smc.dating.get(DeskNoKeyboardView)
            if (view) {
                // 再次允许点击
                view.activeKeyboardOrNot(true)
            }
            smc.dating.remove(DeskNoKeyboardView)
        }
        // 加入麻将
        const openMj = (resp: pb.IDeskJoinInfo) => {
            const afterJoinRoom = () => {
                if (smc.dating.has(DeskNoKeyboardView)) {
                    const view = smc.dating.get(DeskNoKeyboardView)
                    view.close()
                    smc.dating.remove(DeskNoKeyboardView)
                    oops.gui.remove(UIID.deskNoKeyboard)
                }
            }
            smc.gameMj.addInitComp(resp.deskInfo, resp.playerList, resp.categoryConf, false, [],afterJoinRoom, null)
        }
        const joinDesk = (resp: pb.IDeskJoinInfo) => {
            switch (resp.gameType) {
                case GameType.mj:
                    openMj(resp)
                    break
            }
            // 大厅删除本组件，隐藏键盘
            smc.dating.remove(JoinDeskComp)
        }
        if (comp != null && comp.deskNo != null) {
            api.deskJoin({
                deskNo: comp.deskNo
            }, joinDesk, removeJoinDeskComp, true)
        } else {
            console.error("no desk to query")
        }
    }
}